package com.xiyuan.Entity2Json;

import com.xiyuan.Entity2Json.bean.JsonResult;

import java.lang.reflect.Field;

/**
 * 将当前对象以及他的父类转为json
 */
public class SuperEntity2JsonStrategy extends AbstractEntity2JsonStrategy {
    public JsonResult toJsonString(Class<?> clazz)   {
//        反射获取字段
        Field[] fields = clazz.getDeclaredFields();
        Field[] declaredFields = clazz.getSuperclass().getDeclaredFields();

        for (Field field : fields) {
            setValue(jsonObject, field);
            jsonResult.addFieldToList(field.getName());
        }
        for (Field field : declaredFields) {
            setValue(jsonObject, field);
            jsonResult.addFieldToList(field.getName());
        }
        jsonResult.setJsonString(jsonObject.toString());
        return jsonResult;
    }

}
